package com.david4.jizhang.client;

import java.util.Arrays;

public class Hash {

    public static void main(String[] args) {
        // String[] strArr1 = { "xiaoxin", "niutou", "shanqiu", "luobo" };
        // String[] strArr2 = { "xiaoxin", "ggg", "shanqiu", "meile", "dddsf", "niutou" };
        String[] strArr1 = { "xiaoxin", "niutou", "shanqiu" };
        String[] strArr2 = { "ggg", "shanqiu", "meile", "dddsf", "niutou", "luobo" };
        int max = strArr1.length > strArr2.length ? strArr1.length : strArr2.length;
        int[][] ary = new int[max * 4][];
        for (int i = 0; i < strArr1.length; i++) {
            int hash = hash(strArr1[i].hashCode());
            int index = indexFor(hash, max * 4);
            if (ary[index] == null) {
                ary[index] = new int[1];
            } else {
                ary[index] = Arrays.copyOf(ary[index], ary[index].length * 2);
            }
            for (int j = 0; j < ary[index].length; j++) {
                if (ary[index][j] == 0) {
                    ary[index][j] = hash;
                }
            }
        }
        for (int i = 0; i < strArr2.length; i++) {
            int hash = hash(strArr2[i].hashCode());
            int index = indexFor(hash, max * 4);
            if (ary[index] != null) {
                for (int j = 0; j < ary[index].length; j++) {
                    if (ary[index][j] == hash) {
                        System.out.println(strArr2[i]);
                    }
                }
            }
        }
    }

    static int hash(int h) {
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }

    static int indexFor(int h, int length) {
        return h & (length - 1);
    }
}